package flagwind.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 测试拦截器
 */
public class HandlerInterceptorTest implements HandlerInterceptor
{
    /**
     * 执行Handler方法之前执行
     *
     * @param:
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @Description:
     * 用于身份认证、身份授权
     * 比如身份认证，如果认证通过表示当前用户没有登陆，需要此方法拦截不再向下执行
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception
    {
        System.out.println("HandlerInterceptorTest......preHandle");

        // return false表示拦截，不向下执行
        // return true表示放行
        return true;
    }

    /**
     * 进入Handler方法之后，返回modelAndView之前执行
     *
     * @param:
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @Description:
     * 应用场景从modelAndView出发：将公用的模型数据(比如菜单导航)在这里
     * 传到视图，也可以在这里统一指定视图
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception
    {
        System.out.println("HandlerInterceptorTest......postHandle");
    }

    /**
     * 执行Handler完成执行此方法
     *
     * @param:
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @Description:
     * 应用场景：统一异常处理，统一日志处理
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception
    {
        System.out.println("拦截器:" + e);
    }
}
